using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Player_Sys;
namespace Solider_Sys{
	public class Solider : Sprite {
		

		
		public Solider_Type s_type;
		public int defense;
		public int damage;
		public Player_Type player_type;
		public int real_attack_frame;

		//public float animate_interval;
		
		private Solider_State cur_state;

		private int _cur_health;
		private int _max_health;
		private int _cur_mana;
		private int _max_mana;
		private float _move_speed;
		private float _rotate_speed;
		private float _attack_interval;
		private float last_attack_time;
		
		
		private float _visual_range;
		private float _attack_range;
		private GameObject enemy_target;
		private Transform enemy_transform;
		private bool aggressive;
		
		private List<Vector3> way_points;
		private Quaternion final_rotation;
		private bool moving_change_goal;
		private bool rotating_change_target;
		private bool is_rotating;
		
		//攻击间隔
		public float attack_interval
		{
			get{return _attack_interval;}
			set{
				if (value > Data_Ref.SOLIDER_ATTACK_INTERVAL_MIN)
					_attack_interval = value;
				else 
					_attack_interval = Data_Ref.SOLIDER_ATTACK_INTERVAL_MIN;
			}
		}
		
		//角色当前生命值
		public int cur_health
		{
			get{return _cur_health;}
			set{
				if (value > 0)
					_cur_health = Mathf.Min(_max_health , value);
				else//死亡
				{
					
				}
					
			}
		}
		
		public int max_health
		{
			get{return _max_health;}
			set{
				if (value > 0)
					_max_health = value;
				else//死亡
				{
					
				}
					
			}
		}
		
		//角色当前法力值
		public int cur_mana
		{
			get{return _cur_mana;}
			set{
				if (value > 0)
					_cur_mana = Mathf.Min(_max_mana , value);
				else
				{
					_cur_mana = 0;
				}
					
			}
		}
		
		public int max_mana
		{
			get{return _max_mana;}
			set{
				if (value > 0)
					_max_mana = value;
				else
				{
					_max_mana = 0;
				}
					
			}
		}
			
		//角色移动速度（米/秒）
		public float move_speed
		{
			get{return _move_speed;}
			set{
				if (value > 0)
					_move_speed = value;
				else
					_move_speed = 0;
			}
		}
		
		//角色旋转速度（度°/秒）
		public float rotate_speed
		{
			get{return _rotate_speed;}
			set{
				if (value > 0)
					_rotate_speed = value;
				else
					_rotate_speed = 0;
			}
		}
		 
		public float attack_range
		{
			get{return _attack_range;}
			set{
				if (value >= Data_Ref.SOLIDER_ATTACK_RANGE_MIN)
					_attack_range = value;
				else
					_attack_range = Data_Ref.SOLIDER_ATTACK_RANGE_MIN;	
			}
		}	
		
		public float visual_range
		{
			get{return _visual_range;}
			set{
				if (value >= Data_Ref.SOLIDER_VISUAL_RANGE_MIN)
					_visual_range = value;
				else
					_visual_range = Data_Ref.SOLIDER_VISUAL_RANGE_MIN;	
			}
		}
		void Awake()
		{
			
		}
		// Use this for initialization
		void Start () {
			animation_spf = new float[10];
			animation_spf[9] = 100;
			move_speed = 10;
			//transform.position = Vector3.Lerp(new Vector3(0,0,0),new Vector3(1,1,1) , 0.5f);
			//StartCoroutine(Move_To_Point(new Vector3(50,0,50)));
			cur_state = Solider_State.Idleing;

			last_attack_time = Data_Ref.LAST_ATTACK_TIME_INITIAL;
			
			//Reset_Cur_Animation(Solider_Animation.Idle_Start , Solider_Animation.Idle_End , false , true);
			//if (real_attack_frame == animation_frame[(int)Solider_Animation.Attack_End])
			//	real_attack_frame -= 1;

			
		}
		
		// Update is called once per frame
		void Update () {
			Check_Cur_State();
			
		}
		
		void FixedUpdate()
		{
			
			//nrenderer.material.color.a =  new  Color(renderer.material.color.r,renderer.material.color.g,renderer.material.color.b,1.0f-0.1f*Time.time);
		}
		
		protected void Check_Cur_State()
		{
			switch(cur_state)
			{
			case Solider_State.Attacking:
			{
				Try_To_Attack();
				break;
			}
			case Solider_State.Moving:
			{
				
				break;
			}
			case Solider_State.Skilling:
			{
				
				break;
			}
			case Solider_State.Dying:
			{
				
				break;
			}
	
			case Solider_State.Idleing:
			{
				if (Has_Enemy_Target())
				{
					if (Target_In_Attack_Range())
						Try_To_Attack();
					else
						Chase_Target();
				}
				else
				{
					if (Find_New_Target())
						if (Target_In_Attack_Range())
							Try_To_Attack();
						else
							Chase_Target();
				}
				break;
			}
			default:
				break;
				
			}		
		}
		
		protected override void Check_Frame_Event()
		{
			if (next_animation_frame == real_attack_frame) 
			{
				Real_Attack();
			}
		}
		
		protected override void Animation_Finish()
		{
			 
		}
		
		
		protected bool Has_Enemy_Target()
		{
			return (enemy_target != null);
		}
		
		protected bool Target_In_Attack_Range()
		{
			return (Vector3.Distance(transform.position , enemy_transform.position) <= attack_range);
		}
		
		protected bool Target_In_Visual_Range()
		{
			return (Vector3.Distance(transform.position , enemy_transform.position) <= visual_range);
		}
		
		protected bool Find_New_Target()
		{
			return false;
		}
		
		public void Chase_Target()
		{
			
		}
		

		
		protected void Try_To_Attack()
		{
			if (Time.time - last_attack_time < attack_interval)
				return;
			else 
			{
				Change_To_State_Attacking();
			}
			
		}
	
		protected virtual void Real_Attack()
		{
			last_attack_time = Time.time;
			
		}
		
		//从当前位置移动到goal_position
		public void Move_To(Vector3 goal_position)
		{
			//if we put the next 2 lines in coroutine,we need to restart coroutine when is_Moving(n=0 n=1);
			//路径点包含终点，不包含起点
			way_points = null;
			Path_Finding( goal_position);
			//移动中改变goal
			if (cur_state == Solider_State.Moving)
			{
				moving_change_goal = true;
			}
			//从其他状态转移到移动状态
			else
			{
				cur_state = Solider_State.Moving;
				StartCoroutine(Move_Coroutine( ));
			}
				
		}
		
		
		private IEnumerator Move_Coroutine( )
		{
			Vector3 start_position = Vector3.zero;
			Vector3 next_position = Vector3.zero;
			float pos_diff_x = 0;
			float pos_diff_unit_x = 0;
			float pos_x_pass = 0;
			//设置新目标点
			while(true)
			{
				start_position = transform.position;
				next_position = way_points[0];
				pos_diff_x = (next_position - start_position).x;
				pos_diff_unit_x = (next_position - start_position).normalized.x;
			    pos_x_pass = 0;

				while(true)
				{
					//移动中更改goal，重置下一路径点
					if(moving_change_goal)
					{
						moving_change_goal = false;
						break;
					}
					//逐渐移动到下一路径点
					if (transform.position != next_position)
					{	
						pos_x_pass += pos_diff_unit_x*move_speed*Time.deltaTime;
						transform.position = Vector3.Lerp(start_position , next_position , pos_x_pass / pos_diff_x);
					}
					//下一路径点到达，设置新的路径点
					else if(Has_Next_Position())
					{
						break;
					}
					//没有新的路径点，抵达终点（moving finish）********************************
					else 
					{
						yield break;	
					}
					yield return null;
				}
			}
		}
		
		
		//让物体X轴对准target_position
		public void Rotate_XAxis_To(Vector3 target_position)
		{	
			//if we put the next 3 lines in coroutine,we need to restart coroutine when is_rotating(n=0 n=1);
			Vector3 dir = target_position - transform.position;
			//只在x-y平面旋转
			dir.z = 0;
			final_rotation = Quaternion.LookRotation( Vector3.forward * flip_y , Vector3.Cross (Vector3.forward * flip_y , dir));
			if (is_rotating)
			{
				rotating_change_target = true;
			}
			else
			{
				is_rotating = true;
				StartCoroutine(Rotate_Coroutine());
			}
   			
		}
		
		private IEnumerator Rotate_Coroutine()
		{
			float total_angle = 0;
			float angle_pass = 0;
			//设置新旋转方向
			while(true)
			{
				total_angle = Quaternion.Angle(transform.rotation , final_rotation);
				angle_pass = 0;
				while(true)
				{
					if (rotating_change_target)
					{
						rotating_change_target = false;
						break;
					}
					//逐渐将X轴转向对准目标的位置
					if (angle_pass < total_angle)
					{
						angle_pass += rotate_speed*Time.deltaTime;
						transform.rotation = Quaternion.Slerp (transform.rotation, final_rotation, angle_pass / total_angle);
					}
					//旋转完成(rotating finish)********************************
					else
					{
						is_rotating = false;
						yield break;	
					}
					
					yield return null;
					
				}
			}
				
		}
			
			
		private void Path_Finding( Vector3 goal_position)
		{
			
		}
		
		
		private bool Has_Next_Position()
		{
			way_points.RemoveAt(0);
			if (way_points[0] != null)
				return true;
			else
				return false;			
		}

		void Change_To_State_Attacking()
		{
			cur_state = Solider_State.Attacking;
		}
			
		void Change_To_State_Moving()
		{
			cur_state = Solider_State.Moving;
		}
		
		void Change_To_State_Idleing()
		{
			
			cur_state = Solider_State.Idleing;
		}
		
		void Change_To_State_Dying()
		{
			cur_state = Solider_State.Dying;
		}
		
		void Change_To_State_Skilling()
		{
			cur_state = Solider_State.Skilling;
		}
		void Change_To_State_Cooling()
		{
			cur_state = Solider_State.Cooling;
		}
		
		
		
		
		
	}
}